package com.dreamtour.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dreamtour.entity.Agencies;
import com.dreamtour.entity.Users;
import com.dreamtour.service.IAgenciesService;
import com.dreamtour.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2024-12-24
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private IUsersService usersService;

    @RequestMapping("/getByPage")
    public IPage<Users> getByPage(Integer pageNum, Integer pageSize) {
        IPage<Users> page = usersService.page(new Page<>(pageNum, pageSize));
        return page;
    }

    @RequestMapping("/getByName")
    public List<Users> getByName(String uName) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("uName", uName);
        List<Users> list = usersService.list(queryWrapper);
        return list;
    }

    // 统计今天登录用户人数
    @GetMapping("/getTodayLogins")
    public ResponseEntity<Integer> getDailyActiveUsers() {
        int count = usersService.countLoginsBetween(
                LocalDateTime.now().toLocalDate().atStartOfDay(),  // 今天零点
                LocalDateTime.now().toLocalDate().atTime(23, 59, 59)  // 今天23:59:59
        );
        return ResponseEntity.ok(count);
    }

    // 统计昨天登录用户人数
    @GetMapping("/getYesterdayLogins")
    public ResponseEntity<Integer> countYesterdayLogin() {
        int count = usersService.countLoginsBetween(
                LocalDateTime.now().minusDays(1).toLocalDate().atStartOfDay(),  // 昨天零点
                LocalDateTime.now().minusDays(1).toLocalDate().atTime(23, 59, 59)  // 昨天23:59:59
        );
        return ResponseEntity.ok(count);
    }

    @GetMapping("/getDailyRegistrations")
    public List<Map<String, Object>> getDailyRegistrations() {
        return usersService.getDailyRegistrations();
    }

    @GetMapping("/getAgeDistribution")
    public List<Map<String, Object>> getAgeDistribution() {
        return usersService.getAgeDistribution();
    }

    @GetMapping("/getTotalCount")
    public ResponseEntity<Integer> getTotalCount() {
        int count = (int) usersService.count();
        return ResponseEntity.ok(count);
    }

    @GetMapping("/filter")
    public ResponseEntity<Map<String, Object>> filterUsers(
            @RequestParam(required = false) String uRole) {

        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();

        // 添加验证状态筛选条件
        if (uRole != null && !uRole.isEmpty()) {
            queryWrapper.eq("uRole", uRole);
        }

        // 查询符合条件的旅行社
        List<Users> filteredUsers = usersService.list(queryWrapper);

        // 准备返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("users", filteredUsers);

        return ResponseEntity.ok(response);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody Users users) {
        boolean b = usersService.updateById(users);
        return b;
    }

    @RequestMapping("/getUsersCountByMonth")
    public ResponseEntity<List<Map<String, Object>>> getUsersCountByMonth() {
        List<Map<String, Object>> monthlyCounts = usersService.countUsersByMonth();

        // 直接返回原始的统计数据列表
        return ResponseEntity.ok(monthlyCounts);
    }
}
